﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Data;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using QuanLyKho.DKChung;

namespace QuanLyKho.QuanLyKho
{
    public partial class Kho_Khohang_VatTu : Form
    {
        public static string IDKhoHang = "0";

        public Kho_Khohang_VatTu()
        {
            InitializeComponent();
            if (IDKhoHang != "0")
            {
                gridVattu.DataSource = DataAll.SelectAll("Kho_KhoHang_VatTu INNER JOIN Kho_VatTu ON Kho_KhoHang_VatTu.IDVatTu = Kho_VatTu.IDVatTu INNER JOIN Kho_LoaiVatTu ON Kho_VatTu.IDLoaiVatTu = Kho_LoaiVatTu.IDLoaiVatTu",
                    "Kho_LoaiVatTu.[TenLoaiVatTu] AS 'IDLoaiVatTu',Kho_VatTu.[MaVatTu],Kho_VatTu.[TenVatTu],Kho_KhoHang_VatTu.[SoLuong],Kho_VatTu.[DonVi],Kho_VatTu.[MauSac],Kho_VatTu.[KhoGSD],Kho_VatTu.[QCGiac],Kho_VatTu.[MoTa],Kho_VatTu.[GhiChu]",
                    "WHERE IDKhoHang = " + IDKhoHang);
                DataTable dt = DataAll.SelectAll("Kho_KhoHang", "*", "WHERE IDKhoHang = " + IDKhoHang);
                if (dt.Rows.Count > 0)
                {
                    txtMaKho.Text = dt.Rows[0]["MaKho"].ToString();
                    txtTenKho.Text = dt.Rows[0]["TenKho"].ToString();
                    txtMoTa.Text = dt.Rows[0]["MoTa"].ToString();
                }
            }
            luMaDangAnh.Properties.DataSource = DataAll.SelectAll("Kho_VatTu", "MaVatTu", "");
            luMaDangAnh.Properties.Columns.Add(new LookUpColumnInfo("MaVatTu"));
            luMaDangAnh.Properties.DisplayMember = "MaVatTu";
            luMaDangAnh.Properties.ValueMember = "MaVatTu";
            luMaDangAnh.Properties.TextEditStyle = TextEditStyles.DisableTextEditor;
            luMaDangAnh.Properties.NullText = "--- Mã vật tư ---";
            luMaDangAnh.Properties.ShowHeader = false;
        }

        private void luMaDangAnh_EditValueChanged(object sender, EventArgs e)
        {
            var lucheck = (LookUpEdit)sender;
            if (lucheck.ItemIndex >= 0)
            {
                LoadImage();
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void pbAnh_Click(object sender, EventArgs e)
        {
            if (luMaDangAnh.EditValue == null)
            {
                return;
            }
            XemAnh.MaID = luMaDangAnh.EditValue.ToString();
            var anh = new XemAnh();
            anh.ShowDialog();
            XemAnh.MaID = "0";
        }

        public void LoadImage()
        {
            var data = DataAll.SelectAll("DkChung_HinhAnh", "*", " WHERE MaAnh='" + luMaDangAnh.EditValue + "'");
            if (data.Rows.Count > 0)
            {
                var content = (byte[])data.Rows[0]["Anh"];
                if (content != null)
                {
                    Image newImage;
                    using (var ms = new MemoryStream(content, 0, content.Length))
                    {
                        ms.Write(content, 0, content.Length);
                        newImage = Image.FromStream(ms, true);
                    }
                    pbAnh.Image = newImage;
                }
                else
                {
                    pbAnh.Image = null;
                }
            }
            else
            {
                MessageBox.Show("Không tìm thấy mã ảnh", "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
